package test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class MusicDownloaderServer {
    public static void main(String[] args) {
        try {
            // 创建服务器Socket并监听指定端口
            ServerSocket serverSocket = new ServerSocket(8888);
            System.out.println("服务器已启动，等待客户端连接...");

            while (true) {
                // 等待客户端连接
                Socket socket = serverSocket.accept();
                System.out.println("客户端已连接：" + socket.getInetAddress().getHostAddress());

                // 获取输入输出流
                BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);

                // 读取客户端发送的搜索关键字
                String keyword = reader.readLine();
                System.out.println("收到搜索关键字：" + keyword);

                // 根据关键字搜索音乐文件，返回搜索结果
                List<String> searchResult = searchMusic("D:\\music",keyword);
                writer.println(searchResult);

                // 读取客户端发送的要下载的文件名
                String fileName = reader.readLine();
                System.out.println("收到下载请求：" + fileName);

                // 读取文件内容并发送给客户端
                String filePath = "D:\\aaa\\" + fileName;  // 指定目录的路径
                FileInputStream fileInputStream = new FileInputStream(new File(filePath));
                BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
                OutputStream outputStream = socket.getOutputStream();
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
                fileInputStream.close();

                // 关闭连接
                socket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 根据关键字搜索音乐文件，这里只是一个示例方法，需要根据具体情况实现
//    private static String searchMusic(String keyword) {
//        // 搜索并返回音乐文件列表
//        return "音乐文件1.mp3, 音乐文件2.mp3, 音乐文件3.mp3";
//    }
    private static List<String> searchMusic(String directoryPath, String keyword) {
        List<String> musicFiles = new ArrayList<>();
        File directory = new File(directoryPath);

        if (directory.exists() && directory.isDirectory()) {
            searchMusicInDirectory(directory, keyword, musicFiles);
        }

        return musicFiles;
    }

    private static void searchMusicInDirectory(File directory, String keyword, List<String> musicFiles) {
        File[] files = directory.listFiles();

        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    searchMusicInDirectory(file, keyword, musicFiles);
                } else if (file.getName().toLowerCase().contains(keyword.toLowerCase())) {
                    musicFiles.add(file.getName());
                }
            }
        }
    }
}
